import json
import os
import re

#将类对象，转化成json
class json_obj(object):
    def __init__(self,basicConfig={},directory='/ads',location={},maskedParams=[],name='',nodes=[],processType='BATCH',resouces=[],schedule={},singleNodeJobFlag='false',updateLineageInfo='false',useCdmCache='false',version='1.0'):
      self.basicConfig =basicConfig 
      self.directory=directory
      self.location=location
      self.maskedParams=maskedParams
      self.name=name
      self.nodes=nodes
      self.processType=processType
      self.resouces=resouces
      self.schedule=schedule
      self.singleNodeJobFlag=singleNodeJobFlag
      self.updateLineageInfo=updateLineageInfo
      self.useCdmCache=useCdmCache
      self.version=version

class node_obj(object):
    def __init__(self,failPolicy='FAIL_CHILD',location={},maxExecutionTime=360,name='',pollingInterval='5',preNodeName=[],properties=[],resouces=[],retryInterval=120,retryTimes=0,type='DWSSQL'):
        self.failPolicy=failPolicy
        self.location=location
        self.maxExecutionTime=maxExecutionTime
        self.name=name
        self.pollingInterval=pollingInterval
        self.preNodeName=preNodeName
        self.properties=properties
        self.resouces=resouces
        self.retryInterval=retryInterval
        self.retryTimes=retryTimes
        self.type=type
    def get_values(self):
        ls=[]
        ls.append(self.__dict__)
        return ls

class schedule_obj(object):
    def __init__(self,cron,type='CRON'):
        self.cron = cron
        self.type=type
    pass

# 节点名称
class nodes_properties(object):
    def __init__(self,name='scriptArgs',value='',script_name=''):
        self.name = name
        self.value=value
        self.script_name=script_name
        pass
    def get_values(self):
        nodes_properties_list=[]
        nodes_properties_list.append({'name':'connectionName','value':'newhope_fdc_prd'})
        nodes_properties_list.append({'name':'database','value':'fdc_dc'})
        nodes_properties_list.append({'name':'statementOrScript','value':'SCRIPT'})
        nodes_properties_list.append({'name':'scriptName','value':self.script_name})
        nodes_properties_list.append({'name':self.name,'value':self.value})
        return nodes_properties_list
        
# json
basicConfig_dict=dict()
basicConfig_dict['agency']=''
basicConfig_dict['customFields']={}
basicConfig_dict['executeUser']=''
basicConfig_dict['instanceTimeout']=0
basicConfig_dict['owner']='tianchuan'
basicConfig_dict['priority']=0
location_dict={}
location_dict['x']='511'
location_dict['y']='313'

# schedule
schedule_cron_dependJobs=dict()
schedule_cron_dependJobs['dependFailPolicy']='FAIL'
schedule_cron_dependJobs['dependPeriod']='SAME_PERIOD'
schedule_cron_dependJobs['jobs']=[]
schedule_cron_dependJobs['sameWorkSpaceJobs']=[]

schedule_cron=dict()
schedule_cron['concurrent']=1
schedule_cron['dependJobs']=schedule_cron_dependJobs
schedule_cron['dependPrePeriod']='false'
schedule_cron['expression']='0 0 0-23/1 * * ?'
schedule_cron['expressionTimeZone']='GMT+08:00'
schedule_cron['startTime']='2022-03-23T17:53:18+08'


# nodes
node_location=dict()
node_location['x']='-26.0'
node_location['y']='-88.0'

schedule_cron_obj=schedule_obj(cron=schedule_cron).__dict__

def get_paras(file_path,paras_value):
       paras_str=str()
       with open(file=file_path,mode='r',encoding='utf-8') as rf:
           lines=''.join(rf.readlines())
           sql_str=''
           try:
              sql_str=json.loads(lines)['content']
           except Exception:
               print(json.loads(lines))
           paras= re.findall(r'(\$\{.*\})',sql_str)
           for p in set(paras):
               rp= str(p).replace('$','').replace('{','').replace('}',paras_value)
               if len(paras_str)>1:
                   paras_str=paras_str+'\n'+str(rp)
               else:
                   paras_str=rp
       return paras_str
files=[]
# 迭代查找多成目录文件
def get_file_path(path):
    #是目录则查找目录继续查找
    if os.path.isdir(path):
        for file in os.listdir(path):
            get_file_path(path+'\\'+file)
    else:
        #是文件则加入列表
        files.append(path)


if __name__ == '__main__':
    paths=r'C:\Users\me\Downloads\scripts (2)\scripts'
    out_paths=r'C:\Users\me\Desktop\工作日志\job_out1'
    paras_value='''=#{DateUtil.format(Job.planTime,'yyyy-MM-dd')}'''
    # files=os.listdir(path=paths)
    get_file_path(paths)
    out_file=str()
    for file in files:
       file_name=file.split('\\')[-1].split('.script')[0]
       #script脚本路径
       file_path=os.path.join(paths,file)
    #    print(file_path)
       out_file_path=os.path.join(out_paths,file_name.strip().lower()+'.job')
       #实例化对象名称
       obj=json_obj(basicConfig=basicConfig_dict,location=location_dict,name=file_name,schedule=schedule_cron_obj)
       node_objs=node_obj(location=node_location,name=file_name)
       # 获取参数
       if os.path.isfile:
           paras_str=get_paras(file_path,paras_value)
       print(out_file_path)
       #组装node 属性节点
       nodes_properties_obj=nodes_properties(value=paras_str,script_name=file_name)
       node_objs.properties=nodes_properties_obj.get_values()
       obj.nodes=node_objs.get_values()
       with open(file=out_file_path,mode='w',encoding='utf-8') as wf:
            wf.write(json.dumps(obj.__dict__,ensure_ascii=False))
